package com.rv.admin.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.digest.MD5;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rv.admin.constants.RoomType;
import com.rv.admin.domain.Room;
import com.rv.admin.helper.LoginHelper;
import com.rv.admin.mapper.RoomMapper;
import com.rv.admin.nats.publisher.INATSRoomPublisher;
import com.rv.admin.service.IRoomService;
import com.rv.common.core.util.RsaUtil;
import com.rv.common.core.domain.R;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.UUID;

@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements IRoomService {


    @Resource
    private INATSRoomPublisher publisher;


    @Override
    public R<Long> createRoom(Room room) {
        if (room == null) {
            return R.error("房间信息不能为空");
        }
        if (room.getName() == null || room.getName().isEmpty()) {
            return R.error("房间名称不能为空");
        }
        if (room.getType() == null || room.getType() <= 0) {
            return R.error("房间类型不正确");
        }
        if (room.getType().intValue() == RoomType.PRIVATE) {
            if (room.getPassword() == null || room.getPassword().isEmpty()) {
                return R.error("私密房间,密码不能为空");
            }
        }

        Long userId = LoginHelper.getLoginUser().getId();

        LambdaQueryWrapper<Room> lqw = Wrappers.lambdaQuery();
        lqw.eq(Room::getName, room.getName());
        lqw.eq(Room::getType, room.getType());
        lqw.eq(Room::getOwnerId ,userId);
        if (getOne(lqw) != null) {
            return R.error("相同类型房间名称已存在");
        }
        room.setCreateTime(DateUtil.now());
        room.setOwnerId(userId);
        room.setRoomCode(UUID.randomUUID().toString());
        save(room);
        return R.ok(room.getId());
    }

    @Override
    public R<Room> queryRoomById(Long id) {
        Room room = getById(id);
        if (room == null) {
            return R.error("房间不存在");
        }
        return R.ok(room);
    }


    @Override
    public R<Boolean> dismissRoom(Long roomId) {
        Room room = getById(roomId);
        if (room == null) {
            return R.error("房间不存在");
        }
        if (room.getOwnerId().intValue() != LoginHelper.getLoginUser().getId().intValue()) {
            return R.error("只有房间创建人才能解散房间");
        }
        removeById(roomId);
        publisher.publishRoomDismiss(roomId , room);
        return R.ok(true);
    }

    /**
     * 编辑房间
     *
     * @param room 房间信息
     * @return 操作结果
     */
    @Override
    public R<Boolean> editRoom(Room room) {
        if (room == null){
            return R.error("修改房间,提交数据不能为空");
        }
        Room roomDB = getById(room.getId());
        if (roomDB == null){
            return R.error("房间不存在,修改失败");
        }
        LambdaQueryWrapper<Room> lqw = Wrappers.lambdaQuery();
        lqw.eq(Room::getName, room.getName());
        lqw.eq(Room::getType, room.getType());
        lqw.ne(Room::getId, room.getId());
        lqw.eq(Room::getOwnerId ,LoginHelper.getLoginUser().getId());
        if (getOne(lqw) != null) {
            return R.error("相同类型房间名称已存在");
        }
        LambdaUpdateWrapper<Room> luw = Wrappers.lambdaUpdate();
        luw.eq(Room::getId , room.getId());
        luw.set(Room::getName , room.getName());
        luw.set(Room::getImage , room.getImage());
        luw.set(Room::getType , room.getType());
        luw.set(Room::getDescription , room.getDescription());
        if (room.getType() == RoomType.PRIVATE){
            if (ObjectUtil.isEmpty(room.getPassword())){
                return R.error("密码不能为空");
            }
            luw.set(Room::getPassword , room.getPassword());
        }
        update(luw);
        return R.ok();
    }

    /**
     * 校验房间密码
     *
     * @param room 房间信息
     * @return 校验结果
     */
    @Override
    public R<Boolean> verifyPassword(Room room) {
        if (room == null || room.getId() == null) {
            return R.error("房间信息不能为空");
        }
        Room roomDb = getById(room.getId());
        if (roomDb == null) {
            return R.error("房间不存在");
        }
        // 公开房间,无需校验
        if (roomDb.getType().intValue() == RoomType.PUBLIC){
            return R.ok(true);
        }
        // 私密房间,校验密码
        if (roomDb.getPassword().equals(room.getPassword())){
            return R.ok(true);
        }
        return R.error("密码错误");
    }


    @Override
    public R<Page<Room>> queryPublicRoomList(Page<Room> page, Room room) {
        Page<Room> roomPage = baseMapper.queryPublicRoomPage(page, room);
        return R.ok(roomPage);
    }

    @Override
    public R<Page<Room>> queryOwnerRoomList(Page<Room> page, Room room) {
        if (room == null) {
            room = new Room();
        }
        room.setOwnerId(LoginHelper.getLoginUser().getId());
        Page<Room> roomPage = baseMapper.queryOwnerRoomPage(page, room);
        return R.ok(roomPage);
    }


}
